<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_CPanel extends Controller_Template {
    
    public $template = 'cpanel/template';
    public $required_role = 'admin';
    public $redirect_login = null;
    
    public $title = null;
    public $menu = array();
    public $content = '';
    public $messages = array();

    public function before() {
        set_error_handler(array($this, 'errorHandler'));
        
        $this->redirect_login = Route::url('cpanel', array('controller' => 'auth'));

        if ($this->required_role !== null) {
            if (!Auth::instance()->logged_in($this->required_role)) {
                if ($this->redirect_login !== null)
                    Request::current()->redirect($this->redirect_login);
                else
                    die('No access allowed');
            }
        }
        
        $this->menu = array(
            array(__('Settings'), 'main'),
            array(__('Users'), 'users'),
            array(__('Menu'), 'menu'),
            array(__('Pages'), 'pages'),
            array(__('Files'), 'files'),
            array(__('i18n'), 'i18n'),
        );

        parent::before();
    }
    
    public function after() {
        $this->template->title = $this->title;
        $this->template->menu = $this->menu;
        $this->template->content = $this->content;
        $this->template->messages = $this->messages;
        
        parent::after();
    }
    
    public function add_message($message, $type = 'info') {
        $this->messages[] = array($type, $message);
    }
    
    public function errorHandler( $errno, $errstr, $errfile, $errline, $errcontext) {
        $this->add_message($errstr, 'error');
    }
    
}